package com.ymatou.iapi.prodstock.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.ActivityProductDto;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.LiveActivityDto;
import com.ymatou.iapi.prodstock.parameter.CatalogActivityPriceDto;
import com.ymatou.iapi.prodstock.parameter.ProductActivityStockDto;
import com.ymatou.iapi.prodstock.parameter.UpdateProductActivityPriceBean;
import com.ymatou.iapi.prodstock.service.UpdateProductActivityPriceCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*更新商品活动价格
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UpdateProductActivityPrice{
	private static UpdateProductActivityPriceBean updateproductactivitypriceBean;
	private static UpdateProductActivityPriceCall updateproductactivitypriceCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static ArrayList<CatalogActivityPriceDto> catalogActivityPriceDtoList;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品活动价格");
	}

	@Before
	public void caseUp() {
		updateproductactivitypriceBean=new UpdateProductActivityPriceBean();
		updateproductactivitypriceCall=new UpdateProductActivityPriceCall();
		catalogActivityPriceDtoList = new ArrayList<CatalogActivityPriceDto>();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateproductactivitypriceCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductActivityPrice_001() {
		Logger.start(true,"活动商品，更新商品活动价格，平台活动");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			updateproductactivitypriceBean.setActivityID(activityId);
			updateproductactivitypriceBean.setActivityType(0);
			updateproductactivitypriceBean.setProductActivityId(Integer.valueOf(aids.get(0)));
			updateproductactivitypriceBean.setProductID(productId);
			updateproductactivitypriceBean.setUserID(sellerId);
			CatalogActivityPriceDto catalogActivityPriceDto = new CatalogActivityPriceDto();
			catalogActivityPriceDto.setActivityPrice(new BigDecimal(100));
			catalogActivityPriceDto.setCatalogId(catalogs.get(0));
			catalogActivityPriceDtoList.add(catalogActivityPriceDto);
			updateproductactivitypriceBean.setCatalogList(catalogActivityPriceDtoList);
			updateproductactivitypriceCall.setData(updateproductactivitypriceBean);
			updateproductactivitypriceCall.callService();			
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductActivityPrice_002() {
		Logger.start(true,"活动商品，不同规格更新商品活动价格，平台活动");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			updateproductactivitypriceBean.setActivityID(activityId);
			updateproductactivitypriceBean.setActivityType(0);
			updateproductactivitypriceBean.setProductActivityId(Integer.valueOf(aids.get(0)));
			updateproductactivitypriceBean.setProductID(productId);
			updateproductactivitypriceBean.setUserID(sellerId);
			CatalogActivityPriceDto catalogActivityPriceDto1 = new CatalogActivityPriceDto();
			catalogActivityPriceDto1.setActivityPrice(new BigDecimal(100));
			catalogActivityPriceDto1.setCatalogId(catalogs.get(0));
			catalogActivityPriceDtoList.add(catalogActivityPriceDto1);
			CatalogActivityPriceDto catalogActivityPriceDto2 = new CatalogActivityPriceDto();
			catalogActivityPriceDto2.setActivityPrice(new BigDecimal(200));
			catalogActivityPriceDto2.setCatalogId(catalogs.get(1));
			catalogActivityPriceDtoList.add(catalogActivityPriceDto2);
			updateproductactivitypriceBean.setCatalogList(catalogActivityPriceDtoList);
			updateproductactivitypriceCall.setData(updateproductactivitypriceBean);
			updateproductactivitypriceCall.callService();			
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductActivityPrice_003() {
		Logger.start(true,"活动商品，更新商品活动价格，活动价格参数异常");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			updateproductactivitypriceBean.setActivityID(activityId);
			updateproductactivitypriceBean.setActivityType(0);
			updateproductactivitypriceBean.setProductActivityId(Integer.valueOf(aids.get(0)));
			updateproductactivitypriceBean.setProductID(productId);
			updateproductactivitypriceBean.setUserID(sellerId);
			CatalogActivityPriceDto catalogActivityPriceDto1 = new CatalogActivityPriceDto();
			catalogActivityPriceDto1.setActivityPrice(new BigDecimal(-200));
			catalogActivityPriceDto1.setCatalogId(catalogs.get(0));
			catalogActivityPriceDtoList.add(catalogActivityPriceDto1);
			updateproductactivitypriceBean.setCatalogList(catalogActivityPriceDtoList);
			updateproductactivitypriceCall.setData(updateproductactivitypriceBean);
			updateproductactivitypriceCall.callService();			
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductActivityPrice_004() {
		Logger.start(true,"活动商品，不同规格更新商品活动价格，其中一个活动价参数异常");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			updateproductactivitypriceBean.setActivityID(activityId);
			updateproductactivitypriceBean.setActivityType(0);
			updateproductactivitypriceBean.setProductActivityId(Integer.valueOf(aids.get(0)));
			updateproductactivitypriceBean.setProductID(productId);
			updateproductactivitypriceBean.setUserID(sellerId);
			CatalogActivityPriceDto catalogActivityPriceDto1 = new CatalogActivityPriceDto();
			catalogActivityPriceDto1.setActivityPrice(new BigDecimal(100));
			catalogActivityPriceDto1.setCatalogId(catalogs.get(0));
			catalogActivityPriceDtoList.add(catalogActivityPriceDto1);
			CatalogActivityPriceDto catalogActivityPriceDto2 = new CatalogActivityPriceDto();
			//catalogActivityPriceDto2.setActivityPrice(new BigDecimal(-200));
			catalogActivityPriceDto2.setCatalogId(catalogs.get(1));
			catalogActivityPriceDtoList.add(catalogActivityPriceDto2);
			updateproductactivitypriceBean.setCatalogList(catalogActivityPriceDtoList);
			updateproductactivitypriceCall.setData(updateproductactivitypriceBean);
			updateproductactivitypriceCall.callService();			
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductActivityPrice_005() {
		Logger.start(true,"活动商品，不同规格更新商品活动价格，秒杀活动活动");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			updateproductactivitypriceBean.setActivityID(activityId);
			updateproductactivitypriceBean.setActivityType(1);
			updateproductactivitypriceBean.setProductActivityId(Integer.valueOf(aids.get(0)));
			updateproductactivitypriceBean.setProductID(productId);
			updateproductactivitypriceBean.setUserID(sellerId);
			CatalogActivityPriceDto catalogActivityPriceDto1 = new CatalogActivityPriceDto();
			catalogActivityPriceDto1.setActivityPrice(new BigDecimal(100));
			catalogActivityPriceDto1.setCatalogId(catalogs.get(0));
			catalogActivityPriceDtoList.add(catalogActivityPriceDto1);
			CatalogActivityPriceDto catalogActivityPriceDto2 = new CatalogActivityPriceDto();
			catalogActivityPriceDto2.setActivityPrice(new BigDecimal(200));
			catalogActivityPriceDto2.setCatalogId(catalogs.get(1));
			catalogActivityPriceDtoList.add(catalogActivityPriceDto2);
			updateproductactivitypriceBean.setCatalogList(catalogActivityPriceDtoList);
			updateproductactivitypriceCall.setData(updateproductactivitypriceBean);
			updateproductactivitypriceCall.callService();			
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductActivityPrice_006() {
		Logger.start(true,"活动商品，不同规格更新商品活动价格，不同规格价格不同，秒杀活动");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			//CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 6d, 6, starttime);
			
			CreateLiveActivityBean createliveactivityBean = new CreateLiveActivityBean();
			LiveActivityDto LiveActivity=new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts=new ArrayList<>();
			ActivityProductDto activityProductDto1 = new ActivityProductDto();
			activityProductDto1.setProductId(productId);
			activityProductDto1.setActivityPrice(7d);
			activityProductDto1.setActivityStock(6);
			activityProductDto1.setCatalogId(cids.get(0));
			ActivityProductDto activityProductDto2 = new ActivityProductDto();
			activityProductDto2.setProductId(productId);
			activityProductDto2.setActivityPrice(7d);
			activityProductDto2.setActivityStock(6);
			activityProductDto2.setCatalogId(cids.get(1));
			ActivityProducts.add(activityProductDto1);
			ActivityProducts.add(activityProductDto2);
			
			LiveActivity.setActivityProducts(ActivityProducts);
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime()+1*60*1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			
			createliveactivityBean.getLiveActivity().setActivityProducts(ActivityProducts);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			
		//	LiveActivityDto LiveActivity=createliveactivityBean.getLiveActivity();
			LiveRoomWapper LiveRoomWapper = new LiveRoomWapper();
			//直播秒杀活动id
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			LiveActivity.setLiveActivityId(liveacctivityid);
			//直播秒杀商品在活动中的id
			Integer LiveActivityProductId = LiveRoomWapper.selectLiveactivityproduct(liveacctivityid, LiveActivity.getActivityProducts().get(0).getProductId()).get(0).getLiveActivityProductId();
			
			updateproductactivitypriceBean.setActivityID(liveacctivityid);
			updateproductactivitypriceBean.setActivityType(1);
			updateproductactivitypriceBean.setProductActivityId(LiveActivityProductId);
			updateproductactivitypriceBean.setProductID(productId);
			updateproductactivitypriceBean.setUserID(sellerId);
			
			CatalogActivityPriceDto catalogActivityPriceDto1 = new CatalogActivityPriceDto();
			catalogActivityPriceDto1.setCatalogId(cids.get(0));
			catalogActivityPriceDto1.setActivityPrice(new BigDecimal(2));
			CatalogActivityPriceDto catalogActivityPriceDto2 = new CatalogActivityPriceDto();
			catalogActivityPriceDto2.setCatalogId(cids.get(1));
			catalogActivityPriceDto2.setActivityPrice(new BigDecimal(3));
			
			catalogActivityPriceDtoList.add(catalogActivityPriceDto1);
			catalogActivityPriceDtoList.add(catalogActivityPriceDto2);
			updateproductactivitypriceBean.setCatalogList(catalogActivityPriceDtoList);
			updateproductactivitypriceCall.setData(updateproductactivitypriceBean);
			updateproductactivitypriceCall.callService();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}